/*
 * 	Sound manager for tap ranch.
*/
exports.MMSoundManager = Core.Class.singleton({
	
	classname: 'MMSoundManager',
	
	initialize: function()
	{
		this._isMusicOn = MMStorageManager.getValueFromUserDefault("musicOn"); 
		this._areEffectsOn = MMStorageManager.getValueFromUserDefault("effectsOn");    
		
		if(this._isMusicOn == null || typeof this._isMusicOn == 'undefined')
		{
			this._isMusicOn = "true";
			MMStorageManager.saveToUserDefault("musicOn","true");
		}
		
		if(this._areEffectsOn == null || typeof this._areEffectsOn == 'undefined')
		{
			this._areEffectsOn = "true";
			MMStorageManager.saveToUserDefault("effectsOn","true");
		}
		
//		this._musicVolume = MMStorageManager.getValueFromUserDefault("musicVol"); 
//
//		if(this._musicVolume == null || typeof this._musicVolume == 'undefined')
//		{
//			this._musicVolume = 0.5;
//			MMStorageManager.saveToUserDefault("musicVol",0.5);
//			
//		}
//		
//		this._soundVolume = MMStorageManager.getValueFromUserDefault("soundVol"); 
//		if(this._soundVolume == null || typeof this._soundVolume == 'undefined')
//		{
//			this._soundVolume = 0.5;
//			MMStorageManager.saveToUserDefault("soundVol",0.5);
//		}
		   
		   
//        this._isBGMusicPlaying = false;
 	
        this._activeEffectsDictionary = new GAMutableDictionary();
		this._effectsDictionary = new GAMutableDictionary();
		this._listenerDictionary = new GAMutableDictionary();
	},
	setMusicVolume: function(value){
		
		this._musicVolume = value;
		Audio.Music.setVolume(this._musicVolume);
		//MMStorageManager.saveToUserDefault("musicVol",this._musicVolume);
		//console.log("USMAN >> Music Volume Changed to >> " + MMStorageManager.getValueFromUserDefault("musicVol"));
		
	},
	setSoundVolume: function(value){
		this._soundVolume = value;
		//MMStorageManager.saveToUserDefault("soundVol",this._soundVolume);
		
		//console.log("USMAN >> Sound Volume Changed to >> " + MMStorageManager.getValueFromUserDefault("soundVol"));
	},
	saveVolumeToUserDefault:function(){
		MMStorageManager.saveToUserDefault("soundVol",this._soundVolume);
		MMStorageManager.saveToUserDefault("musicVol",this._musicVolume);
	},
	getMusicState: function()
	{
		//console.log("zaki:: getMusicState"+this._isMusicOn);
		return this._isMusicOn;
	},
	
	getEffectsState: function()
	{
		return this._areEffectsOn;
	},	
	
	setEffectsState: function(value)
	{
		this._areEffectsOn = value; 
	},
	setMusicState: function(value)
	{
		this._isMusicOn = value;
	},
	
	playBackgroundSound: function() 
	{
        if(this._isMusicOn == "true") 
   		{
            Audio.Music.setPath(BG_MUSIC_PATH);
//          Audio.Music.setVolume(this._musicVolume);
            Audio.Music.play();
        }
   	},
   
  	stopBackgroundSound: function()
  	{
        Audio.Music.stop();
   	},
	
	toggleBackgroundMusic: function()
	{    	
    	if (this._isMusicOn == "false")
    	{  		
    		this.stopBackgroundSound();
    		MMStorageManager.saveToUserDefault("musicOn","false");
    	}
    	
    	else 
    	{
    		this.playBackgroundSound();
    		MMStorageManager.saveToUserDefault("musicOn","true");
    	}
	},
	
	toggleEffects: function()
	{    
    	if(this._areEffectsOn == "true")
    	{  		
    		MMStorageManager.saveToUserDefault("effectsOn","false");
    		this._areEffectsOn = "false";
    		//disable sound now
    	}
    	
    	else 
    	{
    		MMStorageManager.saveToUserDefault("effectsOn","true");
    		this._areEffectsOn = "true";
    		//enable sound now
    	}
	},
	
	playEffect:function(file, isLoopEnabled)
	{
		if(this._areEffectsOn == "true")
		{
			if(file) {
				var effect = new Audio.Effect(file);
				if(effect) {
					var tickEffect = new Audio.ActiveEffect(effect);
					if(tickEffect)
					{
						this._effectsDictionary.setObject(file,effect);
						this._activeEffectsDictionary.setObject(file, tickEffect);
						if(isLoopEnabled) 
						{
							tickEffect.setLoops(isLoopEnabled);
						}
						else //if(!isLoopEnabled)
						{
						  var emitter = tickEffect.getPlayCompleteEmitter();
						  var listener = new Core.MessageListener();
						  emitter.addListener(listener,this.stopEffect.bind(this,file));
						  this._listenerDictionary.setObject(file,listener);
						}
						//tickEffect.setVolume(this._soundVolume);
						tickEffect.play();
						
					} 
					else 
					{
						effect.destroy();
					}
				}
			}
		}
	},
	
	stopEffect:function(file)
	{
		
		if(file && this._effectsDictionary) {
			if(this._listenerDictionary)
			{
				var listenerObj = this._listenerDictionary.objectForKey(file);
				if(listenerObj){
                    this._listenerDictionary.removeObjectForKey(file);
					listenerObj.destroy();
				}
			}			
			//NgLogE("Sunilla::TRSoundManager file is " + file)
            var effectObj = this._activeEffectsDictionary.objectForKey(file);
            if(effectObj)
			{
                this._activeEffectsDictionary.removeObjectForKey(file);
				effectObj.stop();
				effectObj.destroy();
			}
			effectObj = this._effectsDictionary.objectForKey(file);
			if(effectObj){
                this._effectsDictionary.removeObjectForKey(file);
				effectObj.destroy();
			}
        }
	},
    
    destroy: function() {
        if(this._effectsDictionary) {
            //// TODO
			var objects = this._effectsDictionary.allValues() , len = objects.length , i = 0;
			for(; i < len ; i++)
			{
				objects[i].destroy();
			}
		    this._effectsDictionary.destroy();
				
            
        }
		if(this._activeEffectsDictionary) {
            //// TODO
			var objects = this._activeEffectsDictionary.allValues() , len = objects.length , i = 0;
			for(; i < len ; i++)
			{
				objects[i].destroy();
			}
		    this._activeEffectsDictionary.destroy();
            
        }
		if(this._listenerDictionary) {
            //// TODO
			var objects = this._listenerDictionary.allValues() , len = objects.length , i = 0;
			for(; i < len ; i++)
			{
				objects[i].destroy();
			}
		    this._listenerDictionary.destroy();
            
        }
		
    }

});